package com.example.forum.utils;


import com.example.forum.config.AppConfig;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class Count {

    @Resource
    private  StringRedisTemplate stringRedisTemplate;

    /**
     * 存储用户登录次数到redis
     * @return
     */
    public  int LoginCount() {
        if(stringRedisTemplate.opsForValue().get(AppConfig.Login_key) == null) {
            stringRedisTemplate.opsForValue().set(AppConfig.Login_key,String.valueOf(300));
            return Integer.parseInt(stringRedisTemplate.opsForValue().get(AppConfig.Login_key));
        }

        String count = stringRedisTemplate.opsForValue().get(AppConfig.Login_key);
        Integer i = Integer.parseInt(count);
        i++;
        stringRedisTemplate.opsForValue().set(AppConfig.Login_key,i.toString());
        return Integer.parseInt(stringRedisTemplate.opsForValue().get(AppConfig.Login_key));
    }

    /**
     * 存储用户访问次数到redis
     * @return
     */
    public  int FW_Count() {
        if(stringRedisTemplate.opsForValue().get(AppConfig.Count_key) == null) {
            stringRedisTemplate.opsForValue().set(AppConfig.Count_key,String.valueOf(300));
            return Integer.parseInt(stringRedisTemplate.opsForValue().get(AppConfig.Count_key));
        }

        String count = stringRedisTemplate.opsForValue().get(AppConfig.Count_key);
        Integer i = Integer.parseInt(count);
        i++;
        stringRedisTemplate.opsForValue().set(AppConfig.Count_key,i.toString());
        return Integer.parseInt(stringRedisTemplate.opsForValue().get(AppConfig.Count_key));
    }
}
